package com.jinxin.platform.cdcardpassmgr.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdcardpassmgr.bean.CdcardpassmgrCard;
import com.jinxin.platform.cdcardpassmgr.common.CommonCodeEntity;
import com.jinxin.platform.cdcardpassmgr.common.ResultEntity;
import com.jinxin.platform.cdcardpassmgr.mapper.CdcardpassmgrCardMapper;
import com.jinxin.platform.cdcardpassmgr.service.ICardpassmgrCardService;
import com.jinxin.platform.cdcardpassmgr.task.CdcardpassmgrQuartzScheduler;
import com.jinxin.platform.cdcardpassmgr.utils.IdWorker;
import com.jinxin.platform.cdcardpassmgr.utils.POIUtil;
import com.jinxin.platform.cdcardpassmgr.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-06-30
 * @Author: Lingnan
 **/
@Service
public class CardpassmgrCardServiceImpl implements ICardpassmgrCardService {
    @Autowired
    private CdcardpassmgrCardMapper cardMapper;
    @Resource
    private IdWorker idWorker;

    @Override
    public CdcardpassmgrCard selectOne(HashMap param) {
        return cardMapper.selectOne(param);
    }

    @Override
    public List<CdcardpassmgrCard> list(HashMap param) {
        return cardMapper.list(param);
    }

    @Override
    public List<CdcardpassmgrCard> listPage(HashMap param) {
        return cardMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return cardMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("记录Id不存在");
        }
        int result = cardMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult deleteAll(String params) {
        if(StringUtils.isEmpty(params)){
            return JsonResult.error("参数不存在");
        }
        JSONObject jsonObject = JSONObject.parseObject(params);
        if(jsonObject.containsKey("rIds")){
            String idArr = jsonObject.getString("rIds");
            List<String> idList = JSONArray.parseArray(idArr, String.class);
            if(idList.size() > 0){
                int num = cardMapper.deleteByIds(idList);
                if (num > 0){
                    return JsonResult.ok("删除成功","");
                }else {
                    return JsonResult.error("删除失败");
                }
            }else {
                return JsonResult.error("删除失败");
            }
        }
        return JsonResult.error("参数错误，删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdcardpassmgrCard card) {
        if (card == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = card.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            String cardNum = card.getCardNum();
            if(!StringUtils.isEmpty(cardNum)){
                HashMap param = new HashMap();
                param.put("cardNum",cardNum);
                CdcardpassmgrCard cardInfo = cardMapper.selectOne(param);
                if(cardInfo != null){
                    return JsonResult.error("卡号已存在");
                }
            }
            card.setId(String.valueOf(idWorker.nextId()));
            count = cardMapper.insert(card);
            msg = "新增成功";
        }else {
            count = cardMapper.updateByKey(card);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public ResultEntity uploadCard(MultipartFile file, String projectId,String projectName) throws Exception {
        List<String[]> products = POIUtil.readExcel(file, 13);
        int succeedCount = 0,failCount = 0;
        for (String[] prod:products){
            String[] productInfo = prod;
            String cardNum = productInfo[0].trim();
            if(StringUtils.isEmpty(cardNum) || !Utils.isNumeric(cardNum) || cardNum.length() != 8){
                continue;
            }
            CdcardpassmgrCard cardDo = new CdcardpassmgrCard();
            cardDo.setId(String.valueOf(idWorker.nextId()));
            cardDo.setProjectId(projectId);
            cardDo.setProject(projectName);
            cardDo.setCardNum(productInfo[0].trim());
            cardDo.setCarNum(productInfo[1].trim());
            cardDo.setPrincipal(productInfo[2].trim());
            cardDo.setPhone(productInfo[3].trim());
            int num = cardMapper.insert(cardDo);
            if(num > 0){
                succeedCount++;
            }else {
                failCount++;
            }
        }
        String msg = "成功导入"+succeedCount+"条，失败"+failCount+"条";
        return new ResultEntity(CommonCodeEntity.SUC_CODE,msg,"" );
    }
}
